package tfc_editor.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import tfc_editor.OnLFileManagement.OnLineDirectory;
import tfc_editor.pastrytools.PastryManager;
import tfc_editor.userManagement.UserUtils;

/**
 * Clase que s'encarrega de mostrar els fitxers d'un usuari o els fitxers compartits d'una xarxa.
 * @author Ismael Gonzalez Burgos
 */
public class FinestraMostrarFitxers extends FinestraTFC implements ActionListener
{

	private static final long serialVersionUID = 1L;
	PastryManager pm;
	OnLineDirectory od;
	boolean Shared;

    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;

    /**
     * Constructor de la classe.
     * @param PM Punter a la clase PastryManager.
     * @param parentEditor Editor pare de la classe.
     * @param shared Indica si volem llegir el directori compartit o no.
     */
    public FinestraMostrarFitxers(PastryManager PM,EditorGUI parentEditor,boolean shared)
    {
    	super(parentEditor);
    	pm = PM;
    	Shared = shared;
    	super.Entrar();
        initComponents();
    }

    /**
     * Funcio que inicialitza els components de la finestra.
     */
    private void initComponents() 
    {
    	jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

		od = new OnLineDirectory(pm);

        if (Shared)
        {
    		od.LoadSharedDirectory();
    		setTitle("Fitxers compartits a la xarxa");
        }
        else
        {
    		od.LoadDirectoryFromUser(UserUtils.ActiveUser);
    		setTitle("Fitxers on-line de l'usuari " + UserUtils.ActiveUser);
        }

		String[] strings = new String[(od.GetNumFiles()*7)+1];

		int i;
		int j=0;
		for (i=0;i<od.GetNumFiles();i++)
		{
			strings[j] = new String("Fitxer:");
			j++;
			strings[j] = new String(od.GetFileEntryFromIndex(i).GetName());
			j++;
			strings[j] = new String("Data creacio:");
			j++;
			strings[j] = new String(od.GetFileEntryFromIndex(i).GetDateCreated());
			j++;
			strings[j] = new String("Hora creacio:");
			j++;
			strings[j] = new String(od.GetFileEntryFromIndex(i).GetHourCreated());
			j++;
			strings[j] = new String(" ");
			j++;
		}

		strings[j] = new String("Nombre total de fitxers:" + od.GetNumFiles());
        jList1 = new javax.swing.JList(strings);
        jScrollPane1.setViewportView(jList1);

        jLabel1.setText("Fitxers:");

        jButton1.setText("Sortir");
        jButton1.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(49, 49, 49)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 469, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
        InitC();
    }

    /**
     * Funcio que es crida quan es produeix un event en un dels nodes GUI que tenim registrats per escoltar-ho.
     * @param arg0 Event que ha generat l'accio.
     */
	public void actionPerformed(ActionEvent arg0)
	{
		if (arg0.getSource()==jButton1)
    	{
			Sortir();
    	}
	}
}
